package com.snk.back.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.snk.back.dao.SysBookDao;
import com.snk.back.service.SysBookService;
import com.snk.common.annotation.RequiredLog;
import com.snk.common.exception.ServiceException;
import com.snk.common.pojo.PageObject;
import com.snk.common.utils.AssertUtils;
import com.snk.pojo.SysBook;
import com.snk.pojo.SysBookCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class SysBookServiceImpl implements SysBookService {

    @Autowired
    private SysBookDao sysBookDao;


    @RequiredLog
    @Override
    public PageObject<SysBookCategory> findPageObjects(String name, Long pageCurrent) {

        AssertUtils.isValid(pageCurrent==null || pageCurrent<1,"当前页码值无效");

        int pageSize=3;
        //设置查询位置以及页面大小
        Page<SysBook> page= PageHelper.startPage(pageCurrent.intValue(),pageSize);
        //查询当前页数据
        List<SysBookCategory> records=
                sysBookDao.findPageObjects(name);

        return new PageObject<>(page.getTotal(), records, pageSize, pageCurrent);
}


    @RequiredLog
    @Override
    public int saveObject(SysBook sysBook) {
        long start=System.currentTimeMillis();
        log.info("start:"+start);
        AssertUtils.isValid(sysBook==null, "保存对象不能为空");
        AssertUtils.isValid(StringUtils.isEmpty(sysBook.getName()),"书籍名不能为空");

        int rows=sysBookDao.insertObject(sysBook);
        return rows;
    }
    @RequiredLog
    @Override
    public Map<String, Object> findObjectById(Integer bookId) {
        //long t1=System.currentTimeMillis();//直接在雷芳芳中添加代码会违背开闭原则
        AssertUtils.isValid(bookId==null || bookId<=0,"参数数据不合法，bookId="+bookId);
        SysBookCategory book=sysBookDao.findObjectById(bookId);
        if(book==null)
            throw new ServiceException("此书籍已经不存在");
 AssertUtils.isServiceValid(book==null, "此书籍已经不存在");



        Map<String,Object> map=new HashMap<>();
        map.put("book", book);


        return map;
    }
    @RequiredLog
    @Override
    public int updateObject(SysBook sysBook) {

        AssertUtils.isValid(sysBook==null, "保存对象不能为空");
        AssertUtils.isValid(StringUtils.isEmpty(sysBook.getName()),"书籍名不能为空");

        //更新用户自身信息
        int rows=sysBookDao.updateObject(sysBook);
        return rows;
    }


}
